<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class MobileFoodService extends ServiceBase {


    public function getList($inputs = array(), $pageCount = null){
        //允许排序的字段名称数组
        $canOrderCols = array('name', 'original_price', 'price', 'count', 'status', 'updated_at', 'created_at');

        $order = $this->getSearchField($inputs, 'order');
        $reverse = $this->getSearchField($inputs, 'reverse', 0 );
        $page = $this->getSearchField($inputs, 'page', 1 );

        $name = str_replace(' ', '%', $this->getSearchField($inputs,  'name', '' ));
        $original_price_start = $this->getSearchField($inputs,  'original_price_start', null );
        $original_price_end = $this->getSearchField($inputs,  'original_price_end', null );
        $price_start = $this->getSearchField($inputs,  'price_start', null );
        $price_end = $this->getSearchField($inputs,  'price_end', null );
        $created_at_start = $this->getSearchField($inputs,  'created_at_end', null );
        $created_at_end = $this->getSearchField($inputs,  'created_at_end', null );
        $updated_at_start = $this->getSearchField($inputs,  'updated_at_start', null );
        $updated_at_end = $this->getSearchField($inputs,  'updated_at_end', null );
        $menu_id = $this->getSearchField($inputs,  'menu_id', null );
        $status = $this->getSearchField($inputs,  'status', null );
        $is_top = $this->getSearchField($inputs,  'is_top', null );

        $entries = Food::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        $entries = $entries->where('count','>',0);

        if(!empty($name)){
            $entries = $entries->where('name', 'like', '%'.$name.'%');
        }

        if(!empty($is_top)){
            $entries = $entries->where('is_top', $is_top);
        }

        if(!empty($original_price_start)){
            $entries = $entries->where('original_price', '>=', $original_price_start);
        }

        if(!empty($original_price_end)){
            $entries = $entries->where('original_price', '<=', $original_price_end);
        }

        if(!empty($price_start)){
            $entries = $entries->where('price', '>=', $price_start);
        }

        if(!empty($price_end)){
            $entries = $entries->where('price', '<=', $price_end);
        }

        if(!empty($created_at_start)){
            $created_at_start = strtotime($created_at_start);
            $created_at_start = date('Y-m-d', $created_at_start).' 00:00:00';

            $entries = $entries->where('created_at', '>=', $created_at_start);
        }

        if(!empty($created_at_end)){
            $created_at_end = strtotime($created_at_end);
            $created_at_end = date('Y-m-d', $created_at_end).' 23:59:59';

            $entries = $entries->where('created_at', '<=', $created_at_end);
        }

        if(!empty($updated_at_start)){
            $updated_at_start = strtotime($updated_at_start);
            $updated_at_start = date('Y-m-d', $updated_at_start).' 00:00:00';

            $entries = $entries->where('updated_at', '>=', $updated_at_start);
        }

        if(!empty($updated_at_end)){
            $updated_at_end = strtotime($updated_at_end);
            $updated_at_end = date('Y-m-d', $updated_at_end).' 23:59:59';

            $entries = $entries->where('updated_at', '<=', $updated_at_end);
        }

        if(!empty($menu_id)){
            $menu_ids = array($menu_id);
            $child_menus = Menu::where('parent_id', $menu_id)->select('id')->get();
            if($child_menus != false){
                $menu_ids = array_merge($child_menus->toArray(), $menu_ids);
            }
            $entries = $entries->whereIn('menu_id', $menu_ids);
        }
        //只显示销售中的餐品
        $entries = $entries->whereIn('status', array(0));

        $pageCount = is_numeric($pageCount) ? $pageCount : Config::get('app.pagenate_num');
        $entries = $entries->paginate($pageCount)
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse)

            ->addQuery('name',$name)
            ->addQuery('original_price_start',$original_price_start)
            ->addQuery('original_price_end',$original_price_end)
            ->addQuery('price_start',$price_start)
            ->addQuery('price_end',$price_end)
            ->addQuery('created_at_start',$created_at_start)
            ->addQuery('created_at_end',$created_at_end)
            ->addQuery('updated_at_start',$updated_at_start)
            ->addQuery('updated_at_end',$updated_at_end)
            ->addQuery('menu_id',$menu_id)
            ->addQuery('status',$status)
            ->addQuery('is_top',$is_top);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
            'name' => $name,
            'original_price_start' => $original_price_start,
            'original_price_end' => $original_price_end,
            'price_start' => $price_start,
            'price_end' => $price_end,
            'created_at_start' => $created_at_start,
            'created_at_end' => $created_at_end,
            'updated_at_start' => $updated_at_start,
            'updated_at_end' => $updated_at_end,
            'menu_id' => $menu_id,
            'status' => $status,
            'is_top' => $is_top,
        );

        return array(
            'entries'=>$entries,
            'links'=>$links,
        );

    }

    public function createFood(){
        $food = new Food();
        $this->parseInput($food,'name');
        $this->parseInput($food,'menu_id');
        $this->parseInput($food,'status');
        $this->parseInput($food,'original_price',0);
        $this->parseInput($food,'price');
        $this->parseInput($food,'count',0);
        $this->parseInput($food,'description','');

        if(!$food->save()){
            $this->errors()->merge($food->errors());
            return false;
        }

        return true;
    }

    public function updateFood($food){

        $this->parseInput($food,'name');
        $this->parseInput($food,'menu_id');
        $this->parseInput($food,'status');
        $this->parseInput($food,'original_price');
        $this->parseInput($food,'price');
        $this->parseInput($food,'count');
        $this->parseInput($food,'description');

        if(!$food->save()){
            $this->errors()->merge($food->errors());
            return false;
        }

        return true;
    }

    public function deleteFood($food){
        if(!$food->delete()){
            $this->errors()->merge($food->errors());
            return false;
        }
        return true;
    }

    public function exportFood(){
        $result = $this->getList();
        $table = $result['entries'];
        $type = Input::get( 'type' );

        $columnTitle = Food::$column;

        switch($type){
            case 'csv':
                $output='';

                $output .=  implode(",",$columnTitle)."\r\n";

                $output .= Food::getCSVContent($table);

                $headers = array(
                    'Content-Type' => "text/csv;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename="food_'.date('Y_m_d_His').'.csv"',
                );

                return array('output'=>rtrim($output, "\r\n"), 'headers'=>$headers);
            case 'excel':

                $output = '<table><thead><tr>';
                foreach($columnTitle as $columnName){
                    $output = $output.'<th>'.$columnName.'</th>';
                }
                $output = $output.'</tr></thead>';

                $output = $output.Food::getExcelContent($table);

                $output = $output.'<tfoot><tr><td colspan="'.count($columnTitle).'"></td></tr></tfoot></table>';

                $headers = array(
                    'Pragma' => 'public',
                    'Expires' => 'public',
                    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
                    'Cache-Control' => 'private',
                    'Content-Type' => "application/vnd.ms-excel;;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename=food_'.date('Y_m_d_His').'.xls',
                    'Content-Transfer-Encoding' => ' binary'
                );
                return array('output'=>$output, 'headers'=>$headers);
        }
        $this->errors()->add('error', '导出失败');
        return false;
    }


    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }

}

